const {
  default: Toast
} = require("../../miniprogram_npm/vant-weapp/toast/toast")
const {
  default: Dialog
} = require("../../miniprogram_npm/vant-weapp/dialog/dialog")
const {
  request
} = require("../../utils/asyncWX")

// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    page: 1,
    limit: 10,
    orderList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })

  },
  onShow() {
    this.getOrderList()
  },

  // 获取订单列表
  async getOrderList() {
    const data = {
      page: 1,
      limit: 10
    }
    try {
      const res = await request({
        url: '/api/getOrder',
        data,
        method: 'post'
      })
      if (res.data.code == 200) {
        if (res.data.data.list.length == 0) {
          Toast('没有更多了')
          this.setData({
            page: this.data.page - 1
          })
        }
        this.setData({
          orderList: res.data.data.list
        })
      }
    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '获取订单列表出错啦',
        icon: 'none'
      })
    }
  },

  // 取消确认
  cancelConfirm(e) {
    const id = e.currentTarget.dataset.id
    const _this = this
    wx.showModal({
      title: '取消提示',
      content: '您确定要取消这个订单吗',
      success(res) {
        if (res.confirm) {
          _this.cancelOrder(id)
        } else if (res.cancel) {

        }
      }
    })
  },

  // 取消订单
  async cancelOrder(id) {
    try {
      const res = await request({
        url: '/api/cancelOrder',
        data: {
          id
        },
        method: 'post'
      })
      if (!res) return
      if (res.data.code == 200) {
        Toast(res.data.msg)
        this.getOrderList()
      }
    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '取消失败',
        icon: 'none'
      })
    }
  },

  // 后端支付接口
  async pay(e) {
    const order_id = e.currentTarget.dataset.id
    try {
      const res = await request({
        url: '/api/pay',
        data: {
          order_id
        },
        method: 'post'
      })
      if (!res) return

      if (res.data.code == 200) {
        this.wxPay(res.data.data)
      }
    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '支付前出错啦',
        icon: 'none'
      })
    }
  },

  // 调起微信支付
  wxPay(data) {
    const _this = this
    wx.requestPayment({
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      timeStamp: data.timeStamp,
      success(res) {
        wx.showToast({
          title: '支付成功',
          icon: 'none'
        })
        _this.getOrderList()
      },
      fail(res) {
        wx.showToast({
          title: '取消支付',
          icon: 'none'
        })
      }
    })
  },

  // 获取订单列表
  async getMoreOrderList() {
    const data = {
      page: this.data.page,
      limit: this.data.limit
    }
    try {
      const res = await request({
        url: '/api/getOrder',
        data,
        method: 'post'
      })
      if (!res) return wx.stopPullDownRefresh();
      if (res.data.code == 200) {
        if (res.data.data.list.length == 0) {
          Toast('没有更多了')
          this.setData({
            page: this.data.page - 1
          })
        }
        this.setData({
          orderList: [...this.data.orderList, ...res.data.data.list]
        })
        wx.stopPullDownRefresh();
      }
    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '获取订单列表出错啦',
        icon: 'none '
      })
    }
  },

  // 查看退款详情
  refundInfo(e) {
    const data = e.currentTarget.dataset.refund
    Dialog.alert({
      title: '退款详情',
      message: `退款日期：${data.refund_at} \n\r 退款理由：${data.refund_reason?data.refund_reason:'商家没有填写理由'}`,
    }).then(() => {
      // on close
    });
  },
  // 跳转到管理端
  navMana() {
    wx.navigateTo({
      url: '/pages/nav-mana/mana',
    })
  },
  // 触底加载更多
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.getMoreOrderList()
  },

    // 用户下拉
    onPullDownRefresh() {
      console.log("正在下拉刷新");
      this.getMoreOrderList()
    },
})